//
//  MusicViewController.m
//  Travel_M_Travel
//
//  Created by APPLE10 on 15-3-21.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MusicViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "AFHTTPSessionManager.h"
#import "NetworkingHandler.h"
#import "MusicDataSource.h"
#import "FirstCollectionViewCell.h"
#import "TypeDetailViewController.h"
@interface MusicViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain)UICollectionView *collectionView;
@end

@implementation MusicViewController

- (void)handlerNet
{
    NSString *str = @"http://mobile.ximalaya.com/m/category_tag_list?category=music&device=iPhone&type=album";
    [NetworkingHandler requestWithUrl:str completion:^(NSDictionary *resultDic) {
        
        
        //        NSLog(@"%@",resultDic);
        
        self.arr = [NSMutableArray array];
        NSArray *list = [resultDic objectForKey:@"list"];
        for (NSDictionary *dic in list) {
            MusicDataSource *mds = [[MusicDataSource alloc] initWithDic:dic];
            [self.arr addObject:mds];
            [mds release];
        }
        
        //        NSLog(@"%@", self.arr);
        [_collectionView reloadData];
    
    }];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self handlerNet];
    
    
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.itemSize = CGSizeMake(80, 100);
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 22.5, 20, 22.5);
    flowLayout.minimumLineSpacing = 10;
    flowLayout.minimumInteritemSpacing = 0;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.view addSubview:_collectionView];
    [_collectionView release];
    
    [_collectionView registerClass:[FirstCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FirstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    MusicDataSource *mds = [self.arr objectAtIndex:indexPath.item];
//    cell.tname.text = mds.tname;
    cell.mds = mds;
//    NSLog(@"~~~~~~~~%@",mds.cover_path);
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    TypeDetailViewController *tdvc = [[TypeDetailViewController alloc] init];
    tdvc.mds = [_arr objectAtIndex:indexPath.item];
//    [self presentViewController:tdvc animated:NO completion:^{
//        
//    }];

    [self.navigationController pushViewController:tdvc animated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
